package com.bw.service.impl;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.bw.domain.ParkManager;
import com.bw.domain.ParkRecord;
import com.bw.mapper.ParkManagerMapper;
import com.bw.mapper.ParkRecordMapper;
import com.bw.service.ParkRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @author bobo棒
 * @date 2025年02月27日 16:35
 */

@Service
public class ParkRecordServiceImpl implements ParkRecordService {
    @Autowired
    ParkRecordMapper parkRecordMapper;
    @Autowired
    ParkManagerMapper parkManagerMapper;
    @Override
    public List<ParkRecord> list(ParkRecord parkRecord) {
        return parkRecordMapper.list(parkRecord);
    }

    @Override
    public int addOrUpdate(ParkRecord parkRecord) {
        //判断是入场还是出场- 根据车牌号和出场时间如果是空则表示入场 反之就是出场
      ParkRecord parkRecord1=  parkRecordMapper.getByPlateNum(parkRecord.getPlateNumber());
        //入场
      if(parkRecord1==null){
          //入场时间
          parkRecord.setInTime(new Date());
         return  parkRecordMapper.insert(parkRecord)  ;
      }else{//出场---计算停车费用
          //7、停车时长根据进场时间和出场时间计算得出
          Date inTime = parkRecord1.getInTime();
          //获取停车分钟数
          long minute = DateUtil.between(inTime, new Date(), DateUnit.MINUTE);
          //出场时间
          parkRecord1.setOutTime(new Date());
          //停车时长
          parkRecord1.setParkingDuration(minute+"分钟");

          //查询该车辆是否是包月车
         String cardType= parkRecordMapper.getMonthlyCard(parkRecord1.getPlateNumber());
         //包月车不收费
         if(!StringUtils.isEmpty(cardType)){
             parkRecord1.setFee(new BigDecimal("0"));
             parkRecord1.setCardType(cardType);
             return  parkRecordMapper.update(parkRecord1)  ;
         }

          cardType="临时车";
          parkRecord1.setCardType(cardType);
          //停车时间小于30分钟免费
          if(minute<=30){
              //费用为0
              parkRecord1.setFee(new BigDecimal("0"));
              return  parkRecordMapper.update(parkRecord1)  ;
          }

          //8、每小时停车收费标准为10元，停车在半个小时之内免费，超过半小时正常计费；计算出应收金额
          //停车价格
          ParkManager  parkManager=   parkManagerMapper.get(parkRecord1.getParkId());
          BigDecimal   price = parkManager.getPrice();
          //不足1小时按1小时收费
           double hour =   Math.ceil(minute * 1.0 / 60);
        //应收
          BigDecimal fee = new BigDecimal(hour).multiply(price);

          //9、优惠金额：停车超过12个小时，则每超出1小时优惠3元，计算优惠金额
           if(hour>12){
               //优惠金额
               BigDecimal discount = new BigDecimal(hour - 12).multiply(new BigDecimal(3));
               //10、实收金额 = 应收金额-优惠金额
               fee =fee.subtract(discount);
           }

           //停车费用
           parkRecord1.setFee(fee);
          return  parkRecordMapper.update(parkRecord1)  ;
      }
    }
}
